//Given the root of a binary tree, return the bottom-up level order traversal of
// its nodes' values. (i.e., from left to right, level by level from leaf to root)
//. 
//
// 
// Example 1: 
//
// 
//Input: root = [3,9,20,null,null,15,7]
//Output: [[15,7],[9,20],[3]]
// 
//
// Example 2: 
//
// 
//Input: root = [1]
//Output: [[1]]
// 
//
// Example 3: 
//
// 
//Input: root = []
//Output: []
// 
//
// 
// Constraints: 
//
// 
// The number of nodes in the tree is in the range [0, 2000]. 
// -1000 <= Node.val <= 1000 
// 
// Related Topics 树 广度优先搜索 
// 👍 446 👎 0

package leetcode.editor.cn;

import java.io.File;
import java.net.URL;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.regex.Pattern;

class P107BinaryTreeLevelOrderTraversalIi {
    public static void main(String[] args) {
        Solution solution = new P107BinaryTreeLevelOrderTraversalIi().new Solution();
        Package aPackage = P107BinaryTreeLevelOrderTraversalIi.class.getPackage();
        URL path_1 = P107BinaryTreeLevelOrderTraversalIi.class.getResource("/");
        System.out.println(path_1);
        String classsesPath_2 = P107BinaryTreeLevelOrderTraversalIi.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        System.out.println(classsesPath_2);

        File file = new File("E:\\IntellijIdeaWorkSpace\\JavaEssential\\java-algs4\\edu\\princeton\\cs\\algs4");
        Pattern pattern = Pattern.compile("^.+\\.(?i)(" + "class" + ")$");
        if (file.isDirectory()) {
            for (File listFile : file.listFiles()) {
                if (pattern.matcher(listFile.getName()).find()) {
                    System.out.println(listFile.getName());
                    listFile.delete();
                }
            }
        }
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        Queue<TreeNode> queue = new ArrayDeque<>();
        List<List<Integer>> result = new ArrayList<>();
        ArrayDeque<List<Integer>> vector = new ArrayDeque<>();
        if (root == null) {
            return result;
        }
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> list = new ArrayList<>();
            while (size-- > 0) {
                TreeNode poll = queue.poll();
                list.add(poll.val);
                if (poll.left != null) {
                    queue.offer(poll.left);
                }
                if (poll.right != null) {
                    queue.offer(poll.right);
                }
            }
            vector.offer(list);
        }
        while (!vector.isEmpty()) {
            result.add(vector.pollLast());
        }
        return result;
    }
}

//leetcode submit region end(Prohibit modification and deletion)

}